/////////////////////////////////////////////////////////////////////////////
//
//  Fifteen Puzzle
//  Kolonitsky Developers Alliance (c) 2012
//  All rights received
//
/////////////////////////////////////////////////////////////////////////////

package view.skins
{

    import flash.display.BlendMode;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.filters.DropShadowFilter;
    import flash.geom.Rectangle;
    import flash.text.AntiAliasType;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFieldType;
    import flash.text.TextFormat;

    import flashx.textLayout.formats.TextAlign;

    import model.types.DirectionName;

    import org.kolonitsky.alexey.ui.SimpleButton;
    import org.kolonitsky.alexey.utils.DateUtil;
    import org.kolonitsky.alexey.utils.NumberUtil;

    import view.events.GameEvent;

    public class PuzzleSkin extends Sprite
    {
        private var _duration:Number = 0;

        public function get duration():Number
        {
            return _duration;
        }

        public function set duration(value:Number):void
        {
            _duration = value;
            tfDuration.text = NumberUtil.toW3CTime(value);
        }

        public function PuzzleSkin()
        {
            tfDuration = createTextField();
            tfDuration.filters = [ new DropShadowFilter(1, 90, 0x000000, 0.5, 2, 2, 1, 1, true, false, true) ];
            addChild(tfDuration);

            btnBack = new SimpleButton("", new ImageAssets.BTN_MENU_BITMAP());
            btnBack.addEventListener(MouseEvent.CLICK, btnBack_clickHandler);
            addChild(btnBack);

            btnTop = createButton();
            btnTop.x = 0;
            btnTop.y = -Config.TILE_HEIGHT;

            btnRight = createButton();
            btnRight.x = Config.TILE_WIDTH;
            btnRight.y = 0;

            btnBottom = createButton();
            btnBottom.x = 0;
            btnBottom.y = Config.TILE_HEIGHT;

            btnLeft = createButton();
            btnLeft.x = -Config.TILE_WIDTH;
            btnLeft.y = 0;

            joystik = new Sprite();
            joystik.blendMode = BlendMode.OVERLAY;
            joystik.addChild(btnTop);
            joystik.addChild(btnRight);
            joystik.addChild(btnBottom);
            joystik.addChild(btnLeft);
            addChild(joystik);

            addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
        }

        public function moveJoystik(x:Number, y:Number):void
        {
            joystik.x = x;
            joystik.y = y;
        }

        public function availableDirection(value:Array):void
        {
            btnTop.visible = value.indexOf(DirectionName.TOP) != -1;
            btnRight.visible = value.indexOf(DirectionName.RIGHT) != -1;
            btnBottom.visible = value.indexOf(DirectionName.BOTTOM) != -1;
            btnLeft.visible = value.indexOf(DirectionName.LEFT) != -1;
        }




        //-------------------------------------------------------------------
        //
        //  Private
        //
        //-------------------------------------------------------------------

        private var tfDuration:TextField;
        private var btnTop:Sprite;
        private var btnRight:Sprite;
        private var btnBottom:Sprite;
        private var btnLeft:Sprite;
        private var btnBack:Sprite;
        private var joystik:Sprite;

        private function createTextField():TextField
        {
            var result:TextField = new TextField();
            result.selectable = false;
            result.type = TextFieldType.DYNAMIC;
            result.autoSize = TextFieldAutoSize.CENTER;
            result.antiAliasType = AntiAliasType.ADVANCED;
            result.multiline = false;
            result.defaultTextFormat = new TextFormat("FifteenNumberFont", "28", 0xDECD87);
            result.mouseEnabled = false;
            return result;
        }

        private function createButton():Sprite
        {
            var result:Sprite = new Sprite();
            result.graphics.beginFill(0xFF0000, 0.0);
            result.graphics.drawRect(0, 0, Config.TILE_WIDTH, Config.TILE_HEIGHT);
            result.useHandCursor = true;
            result.buttonMode = true;
            result.addEventListener(MouseEvent.CLICK, joystik_clickHandler);
            return result;
        }

        private function addedToStageHandler(event:Event):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);

            tfDuration.y = stage.stageHeight - 50;
            tfDuration.x = stage.stageWidth / 2;

            btnBack.x = 20;
            btnBack.y = 20;
        }

        private function btnBack_clickHandler(event:MouseEvent):void
        {
            dispatchEvent(new GameEvent(GameEvent.MENU, true));
        }

        private function joystik_clickHandler(event:MouseEvent):void
        {
            var direction:String = "";
            switch (event.target)
            {
                case btnTop:
                {
                    direction = DirectionName.TOP;
                    break;
                }

                case btnRight:
                {
                    direction = DirectionName.RIGHT;
                    break;
                }

                case btnBottom:
                {
                    direction = DirectionName.BOTTOM;
                    break;
                }

                case btnLeft:
                {
                    direction = DirectionName.LEFT;
                    break;
                }
            }
            dispatchEvent(new GameEvent(GameEvent.MOVE, true, false, direction));
        }

    }
}
